package pacBioSimple;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import pacBiopackage.GetOpt;
import tools.kmer.KmerSet_binary;
import tools.kmer.kmerFunctions;

public class PacBioCheckKmerInteractivMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GetOpt go = new GetOpt(args, "o:k:a:h:n:");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		   
		String filenameOutput="output.txt";
		String filenameKmerObject = "";
		int kmer_count_limit =100000;
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		   else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='k')
		    {
		    	filenameKmerObject=go.optArgGet();
		    }
		    else if((char)ch =='n')
		    {
		    	kmer_count_limit=Integer.valueOf(go.optArgGet());
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameKmerObject.isEmpty()) {
		    System.out.println("Usage: -k Kmer -n kmer_count_limit[ -o outputFile -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		//System.out.println("-a " + filenameAlignment); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("-n " + kmer_count_limit); //NOI18N
		System.out.println("-k " + filenameKmerObject); //NOI18N
		System.out.println("------------------------");
		
		
		//first we have to read the kmersFromthe file
		KmerSet_binary ks = null;
		BufferedReader in;
		try 
		{
//			FileHandler fh = new FileHandler("log");
//			log.addHandler(fh);
			in = new BufferedReader(new FileReader(filenameKmerObject));
			String kmer;
			System.out.println("reading kmers");
			while(in.ready())
			{
				kmer=in.readLine();
				int bla= Integer.valueOf(kmer.split("\t")[0]);
				int k = kmer.indexOf('N');
				
				if(kmer.indexOf('N')==-1 && Integer.valueOf(kmer.split("\t")[0]) >=14 && Integer.valueOf(kmer.split("\t")[0]) <= kmer_count_limit )
				{
					if(ks !=null)
					{
						 ks.addKmer(kmerFunctions.kmerToUse(kmer.split("\t")[1]),Integer.valueOf(kmer.split("\t")[0])); //already checked for revComp....
					}
					else
					{
						ks= new KmerSet_binary(kmer.split("\t")[1].length());
						ks.addKmer(kmerFunctions.kmerToUse(kmer.split("\t")[1]),Integer.valueOf(kmer.split("\t")[0]));
					}
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		boolean end =false;
		while(!end)
		{
			String response=null;
			System.out.println("Please give the mode interactiv or file i or f (to end that program type e");
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			try {
				response=input.readLine();
				System.out.println(response);
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			if(response.startsWith("e"))
			{
				end=true;
			}
			else
			{
				if(response.startsWith("i"))
				{
					System.out.println("Please type the kmer to check against the distribution, to exit this mode type e");
					boolean inner_exit=false;
					while(!inner_exit)
					{
						input = new BufferedReader(new InputStreamReader(System.in));
						try {
							response=input.readLine();
						} catch (IOException e) {
							
							e.printStackTrace();
						}
						if(response.startsWith("e"))
						{
							inner_exit=true;
						}
						else
						{
							int kmer_count=ks.checkKmerDistribution(response);
							System.out.println("KmerValue: "+kmer_count);
							System.out.println("Please type the kmer to check against the distribution, to exit this mode type exit");
						}
					}
				}
				else if(response.startsWith("f"))
				{
					//now we have to read a file, and save the result into a file
					System.out.println("Please type the file location or exit to leave this mode");
					boolean inner_exit=false;
					while(!inner_exit)
					{
						input = new BufferedReader(new InputStreamReader(System.in));
						try {
							response=input.readLine();
						} catch (IOException e) {
							
							e.printStackTrace();
						}
						if(response.startsWith("e"))
						{
							inner_exit=true;
						}
						else
						{
							String filename= response;
							BufferedReader br =null;
							PrintWriter pw = null;
							try 
							{
								br = new BufferedReader(new FileReader(filename));
								pw = new PrintWriter(new BufferedWriter(new FileWriter(filename+"result")));
								while(br.ready())
								{
									StringTokenizer st = new StringTokenizer(br.readLine());
									st.nextToken();
									int kmer_count=ks.checkKmerDistribution(st.nextToken());
									pw.println(kmer_count);
								}
							}
							catch (Exception e) {
								
								e.printStackTrace();
							}
							pw.flush();
							pw.close();
							System.out.println("Please type the file location or exit to leave this mode");
						}
					}
				}
				else
				{
					end=true;
				}
			}
		}
	}

}
